﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing.Design;
using System.Security.Permissions;
using System.Windows.Forms.Design;
using Srvtools;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AjaxTools
{
    public class ExtStaticStringEditor : UITypeEditor
    {
        [PermissionSet(SecurityAction.Demand)]
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        [PermissionSet(SecurityAction.Demand)]
        public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            List<string> objName = new List<string>();
            if (context.Instance is ExtSimpleColumn)
            {
                ExtSimpleColumn extColumn = context.Instance as ExtSimpleColumn;
                AjaxBaseControl baseControl = extColumn.Owner as AjaxBaseControl;

                if (context.PropertyDescriptor.Name == "FieldType")
                    objName.AddRange(new string[] { "string", "int", "float", "boolean", "date" });
                else if (context.PropertyDescriptor.Name == "TextAlign")
                    objName.AddRange(new string[] { "left", "center", "right" });
                else if (context.PropertyDescriptor.Name == "DataField")
                {
                    DataTable srcTab = GetSourceTable(baseControl);
                    if (srcTab != null)
                    {
                        foreach (DataColumn field in srcTab.Columns)
                        {
                            objName.Add(field.ColumnName);
                        }
                    }
                }
                else if (context.PropertyDescriptor.Name == "EditControlId" && extColumn is ExtGridColumn)
                {
                    ExtGridColumn extGridColumn = extColumn as ExtGridColumn;
                    if (extGridColumn.Editor == ExtGridEditor.ComboBox)
                    {
                        foreach (Control ctrl in baseControl.Page.Controls)
                        {
                            if (ctrl is ExtComboBox)
                            {
                                objName.Add(ctrl.ID);
                            }
                        }
                    }
                }
            }
            else if (context.Instance is ExtColumnMatch)
            {
                ExtColumnMatch extColumnMatch = context.Instance as ExtColumnMatch;
                AjaxBaseControl baseControl = extColumnMatch.Owner as AjaxBaseControl;
                if (context.PropertyDescriptor.Name == "DestField")
                {
                    foreach (Control ctrl in baseControl.Page.Controls)
                    {
                        if (ctrl is AjaxGridView)
                        {
                            DataTable srcTab = GetSourceTable(ctrl as AjaxBaseControl);
                            if (srcTab != null)
                            {
                                foreach (DataColumn field in srcTab.Columns)
                                {
                                    objName.Add(field.ColumnName);
                                }
                            }
                            break;
                        }
                    }
                }
                else if (context.PropertyDescriptor.Name == "SrcField")
                {
                    DataTable srcTab = GetSourceTable(baseControl);
                    if (srcTab != null)
                    {
                        foreach (DataColumn field in srcTab.Columns)
                        {
                            objName.Add(field.ColumnName);
                        }
                    }
                }
            }
            else if (context.Instance is IChildSet)
            {
                IChildSet set = context.Instance as IChildSet;
                if (context.PropertyDescriptor.Name == "AutoFillingColumn")
                {
                    foreach (ExtGridColumn col in set.OwnerGrid.Columns)
                    {
                        if (!string.IsNullOrEmpty(col.ColumnName) && col.Visible)
                        {
                            objName.Add(col.ColumnName);
                        }
                    }
                }
                else if (context.PropertyDescriptor.Name == "GridPanel")
                {
                    foreach (Control ctrl in set.OwnerGrid.Page.Controls)
                    {
                        if (ctrl is UpdatePanel || ctrl is Panel)
                        {
                            objName.Add(ctrl.ID);
                        }
                    }
                }
            }
            else if (context.Instance is AjaxGridView)
            {
                AjaxGridView grid = context.Instance as AjaxGridView;
                if (context.PropertyDescriptor.Name == "EditPanelID")
                {
                    foreach (Control ctrl in grid.Page.Controls)
                    {
                        if (ctrl is ExtModalPanel)
                        {
                            objName.Add(ctrl.ID);
                        }
                    }
                }
                else if (context.PropertyDescriptor.Name == "QueryPanelID")
                {
                    foreach (Control ctrl in grid.Page.Controls)
                    {
                        if (ctrl is ExtModalPanel)
                        {
                            objName.Add(ctrl.ID);
                        }
                    }
                }
            }
            else if (context.Instance is ExtComboBox)
            {
                ExtComboBox cmb = context.Instance as ExtComboBox;
                if (context.PropertyDescriptor.Name == "ComboPanel")
                {
                    foreach (Control ctrl in cmb.Page.Controls)
                    {
                        if (ctrl is Panel)
                        {
                            objName.Add(ctrl.ID);
                        }
                    }
                }
            }

            IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (service != null)
            {
                StringListSelector selector = new StringListSelector(service, objName.ToArray());
                string strValue = (string)value;
                if (selector.Execute(ref strValue)) value = strValue;
            }
            return value;
        }

        DataTable GetSourceTable(AjaxBaseControl ctrl)
        {
            if (ctrl is IAjaxDataSource)
            {
                object src = ctrl.GetObjByID(((IAjaxDataSource)ctrl).DataSourceID);
                if (src != null && src is WebDataSource)
                {
                    WebDataSource wds = src as WebDataSource;
                    if (string.IsNullOrEmpty(wds.SelectAlias) && string.IsNullOrEmpty(wds.SelectCommand))
                    {
                        if (wds.DesignDataSet == null)
                        {
                            WebDataSet ds = GloFix.CreateDataSet(wds.WebDataSetID);
                            wds.DesignDataSet = ds.RealDataSet;
                        }
                        return wds.DesignDataSet.Tables[wds.DataMember].Clone();
                    }
                    else
                    {
                        return wds.CommandTable.Clone();
                    }
                }
            }
            return null;
        }
    }
}